
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class MetalWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FlagWarWorld extends World
{
    //The Hero is the legs, while the Body is the body that follows it.
    private Hero player1;
    
    private Body body1;
 
    private int playerSpeed1 = 3;      //Running speed for player 1
    private int direction1 = 1;  
    private Vida lifes[];
   private int puntosJugador;
  
    private int vidas=5;
    private int changeX1 = 0;    
    private int disparos=0;
    
    private boolean inmune;
    private int tiempoInmunidad=400;
    private boolean jugador; //si el jugador esta vivo
    private boolean explosion;
    private int numEnemigos=9;
    private boolean flecha = false;
    private GreenfootImage escenario;
    private boolean nivel2=false;
    private boolean menu;
    private boolean ayuda;
    private int nivel=1;
    private boolean bandera;
    public FlagWarWorld()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 550, 1,false); 
        jugador=true;
        explosion=false;
         lifes=new Vida[5];
        
       
        ayuda=false;
      

        //Setting up the players
        

        //Fixing the order which actors are layered.
        setPaintOrder (Sangre.class, Explosion.class, Body.class, Hero.class, PlayerShot.class, Enemigo.class,Arma.class);
        Greenfoot.setSpeed(50);
        menu();
    }

    public void menu()
    {
        
       jugador=false;
        menu=true;
        removeObjects(getObjects(null));
        setBackground("MENU.png");
        addObject(new Play(),100,510);
        addObject(new Help(),300,510);
        addObject(new Score(),500,510);
        addObject(new Name(),250,70);
        addObject(new Creditos(),675,50);
        
       //   this.PreparaNivel1();
    }
    public void act (){
            if(menu==true)
            {
                checaMenu();
            }
            if(ayuda==false && vidas==0)
            {
                lose();
            }
            if(ayuda==true)
            {
                if(Greenfoot.isKeyDown("space"))
                {
                    menu();
                    ayuda=false;
                    menu=true;
                }
            }
            if(jugador){
            checkKeys();       //Control input
            
            legBodyCommunicate(); //Have the legs send information to the body.
           }
           if(numEnemigos==0  && flecha==false && nivel!=3)
           {
           
            addObject(new Flecha(),700,200);
            flecha=true;
            player1.setRightWall(850);
            }
            
              if(numEnemigos==0 && nivel==3 && bandera==false)
           {
            addObject(new Bander(),760,260);
            
            //addObject(new Flecha(),700,200);
            bandera=true;
            //player1.setRightWall(850);
            }
            if(inmune)
            {
                decrementaInmunidad();
            }
    }

    private void checkKeys (){
        if (player1.jumping() == false){ //Constricts movement if the player is in the air.
            changeX1 = 0;
            if (Greenfoot.isKeyDown("left")){ //Move left
                changeX1 = changeX1 - playerSpeed1;
                direction1 = -1;}
            if (Greenfoot.isKeyDown("right")){ //Move right
                changeX1 = changeX1 + playerSpeed1;
                direction1 = 1;
                  if(player1.getX()>820)
               {
                   if(nivel==1)
                   {
                       preparaNivel2();
                    }
                    else
                    {
                     preparaNivel3();
                    }
                
               }
        
            }
        }
        if (Greenfoot.isKeyDown("up") && player1.jumping() == false){ //Jump
            player1.jump();
        }
        if(Greenfoot.isKeyDown("down") )
        {
            player1.setLocation(player1.getX(),player1.getY()+15);
            
        }
         if((Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("D")) && player1.getY()<520 && player1.sobrePlataforma())   
        {
            player1.setLocation(player1.getX(),player1.getY()+20);
            player1.setJumping();
        }
        

        //Sending movement to the hero (legs) to process sprites with
        player1.moveMe (changeX1, direction1);

        if (Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown ("1")){
            player1.playerFire();
        }   
     
    } 

    public void legBodyCommunicate(){ //Legs and bodies need to communicate for seperate animation sprites.
        body1.statusToBody(player1.statusFromHero());
     
        body1.locationToBody(player1.locationFromHero());
      
    }
    
    public void creaJugador()
    {
        if(vidas==5)
        {
        player1 = new Hero();
        body1=new Body();
       }
        addObject (player1, 150, 72);
        addObject (body1, 155, 42);
        inmune=true;
        
    }
    
    public void removeJugador()
    {
        
        if(vidas==0)
    {
        jugador=false;
        //lose();
    }
     
    if(vidas>0)
    {
        removeObject(player1);
        removeObject(body1);
     
        removeObjects(getObjects(Vida.class));
        vidas--;
    
    for(int i=0;i<vidas;i++)
       {
           lifes[i]=new Vida();
           addObject(lifes[i],30+i*30,20);
      }
     this.creaJugador();
     jugador=true;
     body1.setVivo();
      
    } 
    //this.creaJugador();
        
    }
    
    public int getXJugador()
    {  
        return (body1.getX());
      
      
    }
  
    public void setJugador(boolean exp)
   {
       if(inmune==false){
       jugador=false;
       body1.setMuerto();
       
       if(exp)
       {
           explosion=true;
           this.removeJugador();
       }
    }
   }
   
   public void setImageJugador(int soldado)
   {
       if(inmune==false)
       {
       body1.setImageHerida(soldado);
       Greenfoot.delay(20);
       removeJugador();
    }
   }
   
  public int getXHero()
  {
       return player1.getX();
  }
   public int getYHero()
  {
      return player1.getY();
   }
  public boolean getJugador()
  {
      return jugador;
  }

  public void creaPlataforma(int x ,int y)
  {
      addObject(new Plataforma(180),x,y);
  }

public void cambiaBalas(int tipo)
{
    player1.setBala(tipo);
}
public void menosEnemigos()
{
    numEnemigos--;
}

public void generaBonos()
{
    for(int i=0;i<10;i++)
    {
        int bono = Greenfoot.getRandomNumber(6);
       //System.out.println(bono);
        switch(bono)
        {
            case 1:
            addObject(new Moneda(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
            break;
            
            case 2:
            addObject(new Medalla(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
            break;
            case 4:
            addObject(new Cherry(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
            break;
            case 5:
            addObject(new Apple(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
            break;
            
            
        }
    }
}

public void preparaNivel2()
    {
        nivel=2;
        flecha=false;
        numEnemigos=11;
         int xp=player1.getX();
                  int yp=player1.getY();
                player1.setRightWall(800);
                  removeObjects(getObjects(null));
                  addObject(player1,xp,yp);
                  addObject(body1,xp+30,yp+20);
                  player1.setLocation(30,player1.getY());
        
         
        for(int i=0;i<vidas;i++)
       {
           lifes[i]=new Vida();
           addObject(lifes[i],30+i*30,20);
        }
     
        for(int i=0;i<7;i++)
        {
           addObject(new Plataforma(180,2),80+160*i,595);
        }
        addObject(new Letrero("Puntos :" +puntosJugador),710,10);
        addObject(new Plataforma(200,2),180,90);
        addObject(new Plataforma(200,2),139,205);
        addObject(new Plataforma(200,2),285,345);
        addObject(new Plataforma(200,2),100,455);
         addObject(new Plataforma(260,2),554,188);
        addObject(new Plataforma(260,2),680,108);
        addObject(new Plataforma(260,2),670,285);
        addObject(new Plataforma(260,2),593,426);

            addObject(new Plataforma(800,2),400,545);
             addObject(new SoldadoEspada(70,330,1),250,490);
             addObject(new Soldado(385,645,1),400,497);
             addObject(new SoldadoBasooka(400,660,1),500,487);
             
               addObject(new SoldadoEspada(50,310,1),190,35);
             addObject(new Soldado(9,240,1),170,161);
             addObject(new SoldadoBasooka(155,415,1),308,286);
             
               addObject(new SoldadoEspada(10,230,1),150,399);
             addObject(new Soldado(540,800,1),650,240);
             addObject(new SoldadoBasooka(463,723,1),686,368);
               addObject(new SoldadoEspada(463,7230,1),541,370);
               addObject(new Mina(),379,70);
               for(int i=0;i<3;i++)
               {
                    addObject(new Mina(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
                }
                addObject(new G(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
                     addObject(new C(),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(550));
        addObject(new Allen(-1),640,53);
        
    }
     public void PreparaNivel1()
    {
            puntosJugador=0;
            nivel=1;
            bandera=false;
               removeObjects(getObjects(null));
               addObject(new Letrero("Puntos : 0"),710,10);
             for(int i=0;i<vidas;i++)
       {
           lifes[i]=new Vida();
           addObject(lifes[i],30+i*30,20);
        }
         GreenfootImage escenario=new GreenfootImage("Nivel1.png");
        
        escenario.scale(1000,600);
        setBackground(escenario);
        addObject(new C(), 780,560);
        this.creaJugador();
        cambiaBalas(1);
            addObject(new Plataforma(800,1),400,545);
            addObject(new Plataforma(260),200,100);
            addObject(new SoldadoEspada(70,330,1),250,45);

            addObject(new Plataforma(260),515,71);
            addObject(new Soldado(385,645,1),400,38);

            addObject(new Plataforma(260),530,170);
            addObject(new SoldadoBasooka(400,660,1),500,110);

            addObject(new Plataforma(260),220,250);
            addObject(new SoldadoEspada(90,350,1),300,195);

            addObject(new Plataforma(260),643,268);
            addObject(new SoldadoBasooka(385,645,1),550,290);
         

            addObject(new Plataforma(260),515,350);
             addObject(new Soldado(513,763,1),532,215);
        
            addObject(new Plataforma(260),160,455);
            addObject(new Soldado(30,290,1),100,403);

            
            addObject(new Soldado(370,630,2),450,493);

            addObject(new Plataforma(260),645,434);
            addObject(new SoldadoEspada(515,775,1),741,378);

        
    }
    public boolean getInmunidad()
    {
        return inmune;
    }
    public void decrementaInmunidad()
    {
        if(tiempoInmunidad==0)
        {
            tiempoInmunidad=400;
            inmune=false;
        }
        else
        {tiempoInmunidad--;
        }
    }
    public void preparaNivel3()
    {
                flecha=false;
        numEnemigos=11;
     
                
                  
                  
                  
        nivel=3;
       
        flecha=false;
        numEnemigos=12;
        setBackground("Nivel2.png");
        repaint();
            int xp=player1.getX();
                  int yp=player1.getY();
                  player1.setRightWall(800);
        removeObjects(getObjects(null));
        addObject(player1,xp,yp);
                  addObject(body1,xp+30,yp+20);
                  player1.setLocation(30,player1.getY());
         addObject(new Letrero("Puntos : "+puntosJugador),710,10);
                  player1.setground(535);
                  addObject(player1,600,400);
                  addObject(body1,630,420);
                   addObject(new SoldadoEspada(900,555,2),900,506);
                  addObject(new SoldadoEspada(900,555,2),800,506);
                  addObject(new SoldadoEspada(900,555,2),700,506);
                  addObject(new SoldadoEspada(900,555,2),500,506);
                  addObject(new SoldadoEspada(900,555,2),300,506);
                    addObject(new SoldadoBasooka(660,960,2),450,511);
            
                      addObject(new SoldadoBasooka(660,960,2),130,511);
            
                  
                  addObject(new Plataforma(300),185,400);
            addObject(new Soldado(35,335,1),140,348);
            
             addObject(new Plataforma(300),810,320);
            addObject(new SoldadoBasooka(660,800,1),765,260);
            
             addObject(new Plataforma(200),479,222);
            addObject(new SoldadoBasooka(329,629,1),586,260);
            
             addObject(new Plataforma(200),470,310);
             addObject(new Plataforma(200),744,210);
              addObject(new Plataforma(200),154,219);
          addObject(new Allen(1),101,163);
          addObject(new Allen(-1),648,162);

            

                  
                   
        
    }
    public void sumaPuntos(int pts)
    {
        puntosJugador+=pts;
        removeObjects(getObjects(Letrero.class));
        addObject(new Letrero("Puntos :"+puntosJugador),710,10);
    }
    public void checaMenu()
    {
           if (Greenfoot.mouseClicked(null))
      {
       MouseInfo mouse = Greenfoot.getMouseInfo();
     
       int x = mouse.getX();
       int y = mouse.getY();
       int btn=mouse.getButton();
        addObject(new Play(),100,510);
        addObject(new Help(),300,510);
        addObject(new Score(),500,510);
        addObject(new Name(),250,70);
       if (btn == 1)
       {
           if(40<x && x<160 && 480<y && y<540)
           {
               menu=false;
              PreparaNivel1();
              jugador=true;
              
           }
           
           if(240<x && x<360 && 480<y && y<540)
           {
               menu=false;
               ayuda=true;
              ayuda();
              //jugador=true;
              
           }
           
           if(400<x && x<600 && 480<y && y<540)
           {
               menu=false;
               ayuda=true;
               
               records();
              //jugador=true;
              
           }
           
           if(575<x && x<775 && 10<y && y<120)
           {
               menu=false;
               ayuda=true;
              portada();
              //jugador=true;
             
        
          }
     }
   }
}
   public void ayuda()
   {
       removeObjects(getObjects(null));
       setBackground("ayuda.png");
      
         addObject(new space(),695,542);
        
    }
    public void portada()
   {
       removeObjects(getObjects(null));
       setBackground("portada.png");
      addObject(new space(),695,542);
         
        
    }
    
    public void lose()
    {  
        jugador=false;
        super.removeObjects(getObjects(Enemigo.class));
           super.removeObjects(getObjects(null));
        
            ScoreBoard scoreboard = new ScoreBoard(getWidth(),getHeight());
            addObject(scoreboard,400,275);
            scoreboard.storeScore(puntosJugador);
            puntosJugador=0;
            Greenfoot.delay(100);
            ayuda=true;
            addObject(new space(),695,542);
        vidas=-1;
        
    }
     public void records()
    {
        jugador=false;
        super.removeObjects(getObjects(null));
        addObject(new ScoreBoard(getWidth(),getHeight()),400,275);
        ayuda=true;
        addObject(new space(),695,542);
        //addObject(new Menu(),299,368);
    }
}



